package com.laery0.xixin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.laery0.xixin.mapper.OrdersMapper;
import com.laery0.xixin.po.Orders;
import com.laery0.xixin.po.ResponseBean;
import com.laery0.xixin.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;

    // 通过 手机号 查询 是否可以预约
    @Override
    public ResponseBean<Integer> getOrdersByUserId(String userId) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("state", 1);
        List<Orders> orders = ordersMapper.selectList(queryWrapper);
        if (orders.size() > 0) {
            return new ResponseBean<>(601, "该用户的预约未归档, 无法预约");
        } else {
            return new ResponseBean<>(orders.size());
        }
    }

    // 保存预约订单并返回自动增长主键
    @Override
    public ResponseBean<Integer> saveOrders(Orders orders) {
        int i = ordersMapper.insert(orders);
        Integer orderId = orders.getOrderId();
        return new ResponseBean<>(orderId);
    }

    // 根据 用户编号 和 订单状态 查询 体检预约列表-体检医院信息
    @Override
    public ResponseBean<List<Orders>> listOrdersByUserId(Orders orders) {
        List<Orders> ordersList = ordersMapper.listOrdersByUserId(orders);
        if (ordersList == null) {
            return new ResponseBean<>(601, "未查询到相关订单");
        } else {
            return  new ResponseBean<>(ordersList);
        }
    }

    // 根据 预约编号 删除 体检预约信息
    @Override
    public ResponseBean<Integer> removeOrders(Integer orderId) {
        int i = ordersMapper.deleteById(orderId);
        if (i == 0) {
            return new ResponseBean<>(601, "未找到id为:" + orderId + "的预约信息");
        } else {
            return new ResponseBean<>(i);
        }
    }

    // 根据 预约编号 查询 体检预约信息
    @Override
    public ResponseBean<Orders> getOrdersById(Integer orderId) {
        Orders orders = ordersMapper.selectById(orderId);
        if (orders == null) {
            return new ResponseBean<>(601, "未找到id为:" + orderId + "的预约信息");
        } else {
            return new ResponseBean<>(orders);
        }
    }
}
